package com.nageoffer.shortlink.project.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nageoffer.shortlink.project.dao.entity.LinkLocaleStatsDo;
import com.nageoffer.shortlink.project.dto.req.ShortLinkStatsReqDTO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 地图统计访问持久层
 */
public interface LinkLocaleStatsMapper extends BaseMapper<LinkLocaleStatsDo> {

    /**
     * 记录地区访问监控数据
     * @param linkLocaleStatsDo
     */
    @Insert("Insert Into t_link_locale_stats (gid, full_short_url, date, cnt,province,city ,country,adcode,create_time, update_time, del_flag)values " +
            "(#{linkLocaleStats.gid},#{linkLocaleStats.fullShortUrl},#{linkLocaleStats.date},#{linkLocaleStats.cnt}" +
            ",#{linkLocaleStats.province},#{linkLocaleStats.city},#{linkLocaleStats.country},#{linkLocaleStats.adcode},now(),now(),0) " +
            "on DUPLICATE KEY UPDATE cnt = cnt +#{linkLocaleStats.cnt}")
    void shortLinkLocaleStats(@Param("linkLocaleStats") LinkLocaleStatsDo linkLocaleStatsDo);


    /**
     * 根据短链接获取指定日期内基础监控数据
     */
    @Select("SELECT " +
            "    province, " +
            "    SUM(cnt) AS cnt " +
            "FROM " +
            "    t_link_locale_stats " +
            "WHERE " +
            "    full_short_url = #{param.fullShortUrl} " +
            "    AND gid = #{param.gid} " +
            "    AND date BETWEEN #{param.startDate} and #{param.endDate} " +
            "GROUP BY " +
            "    full_short_url, gid, province;")
    List<LinkLocaleStatsDo> listLocaleByShortLink(@Param("param") ShortLinkStatsReqDTO requestParam);

}
